#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int cmp1(const int* a, const int* b)
{
	return *a - *b;
}

int cmp2(const int* a, const int* b)
{
	return *b - *a;
}

void print(int* a, size_t size)
{
	for (size_t i = 0; i < size; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}


void Test1()
{
	int a[] = { 1,3,5,7,9,2,4,6,8,10 };
	size_t size = sizeof(a) / sizeof(a[0]);
	qsort(a, size, sizeof(int), cmp1);
	print(a, size);

	qsort(a, size, sizeof(int), cmp2);
	print(a, size);
}

typedef int DataType;

int binSelect(int* a, size_t size, DataType x)
{
	size_t l = 0, r = size - 1, mid;
	while (l <= r)
	{
		mid = (l + r) / 2;

		if (a[mid] == x)
		{
			return mid;
		}
		else if (a[mid] > x)
		{
			r = mid - 1;
		}
		else
		{
			l = mid + 1;
		}
	}

	return -1;
}


//int binSearch(int* a, size_t size, int x)
//{
//	size_t l = 0, r = size - 1, mid;
//
//	while (l <= r)
//	{
//		mid = (r + l) / 2;
//		if (a[mid] == x)
//		{
//			return mid;
//		}
//		else if (a[mid] < x)
//		{
//			l = mid + 1;
//		}
//		else
//		{
//			r = mid - 1;
//		}
//	}
//
//	return -1;
//}

void Test2()
{
	int a[] = { 110,100,120,140,105,101 };
	size_t size = sizeof(a) / sizeof(a[0]);
	qsort(a, size, sizeof(int), cmp1);
	print(a, size);

	printf("%d\n", binSelect(a, size, 110));
	printf("%d\n", binSelect(a, size, 100));
	printf("%d\n", binSelect(a, size, 140));
	printf("%d\n", binSelect(a, size, 11));

}

int main()
{
	Test2();



	return 0;
}